import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import local_get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import { spaceid } from '../../atom/config';
import P01 from './P01';
import P02 from './P02';
// 查询条件
interface Obj {
	filter: {};
	page_size: number;
	page_no: number;
}
// 列表加载+查询
export default async function a002(fd: IFeidaoAiBrowserComponent) {
	const page_no = Number(!local_get(fd, 'page_no') ? 1 : local_get(fd, 'page_no'));// 初始化页码
	const page_size = 10;// 初始化每页条数

	const obj = {} as Obj;
	const filter = {} as { client_type: string, name: {}; productid: string; };
	filter.productid = spaceid;

	if (local_get(fd, 'name_key')) {
		const name_key = local_get(fd, 'name_key');
		filter.name = {
			$regex: '.*' + name_key + '.*',
			$options: "i"
		};
	}
	filter.client_type = local_get(fd, 'client_type');
	obj.filter = filter;
	obj.page_no = page_no;
	obj.page_size = page_size;
	// 查询
	const res = await nodejs<{ dataArray: Array<{}>, count: number, page_no: number, page_size: number }>('function-definition/zj-000006/s001', obj);
	// 渲染数据
	if (res.count > 0) {
		// 如果数据总条数大于每页条数就显示分页
		if (res.count > page_size) {
			res.page_no = page_no; // 设置第几页
			res.page_size = page_size; // 设置每页条数
			render(fd, res, P02, 'P02', 'inner');
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.remove('display-n');
		} else {
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.add('display-n');
		}
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.remove('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[wushujv]').classList.add('display-n');
		render(fd, res, P01, 'P01', 'inner');
	} else {
		// 如果没数据 展示空蒙版
		fd.data.node.querySelector<HTMLDivElement>('[wushujv]').classList.remove('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.add('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.add('display-n');
	}
}
